import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/layout/layout.vue";
import { useUserStore } from "@/store";

const routes = [
  {
    path: "",
    component: Layout,
    redirect: "/index",
    children: [
      {
        name: "Index",
        path: "index",
        component: () => import("@/views/index/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "Supply",
        path: "supply",
        component: () => import("@/views/supply/index.vue"),
      },
      {
        name: "SupplyDetail",
        path: "supply/detail",
        component: () => import("@/views/supply/detail.vue"),
      },
      {
        name: "CreateSupply",
        path: "supply/create",
        component: () => import("@/views/supply/createSupply.vue"),
      },
      {
        name: "SupplyApply",
        path: "supply/apply",
        component: () => import("@/views/supply/apply.vue"),
      },
      {
        name: "CreateDemand",
        path: "supply/createDemand",
        component: () => import("@/views/supply/createDemand.vue"),
      },
      {
        name: "DataSource",
        path: "dataSource",
        component: () => import("@/views/dataSource/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        name: "ModelSquare",
        path: "model/square",
        component: () => import("@/views/model/square.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        name: "ModelDetail",
        path: "model/square/detail",
        component: () => import("@/views/model/modelDetail.vue"),
      },
      {
        name: "CreateModel",
        path: "model/create",
        component: () => import("@/views/model/createElement.vue"),
      },
      {
        name: "CreateDataSource",
        path: "dataSource/create",
        component: () => import("@/views/model/createElement.vue"),
      },
      {
        name: "ModelBase",
        path: "model/base",
        component: () => import("@/views/model/base.vue"),
      },
      {
        name: "ModelActive",
        path: "model/active",
        component: () => import("@/views/model/experience.vue"),
      },
      {
        name: "Info",
        path: "info",
        component: () => import("@/views/info.vue"),
      },
      {
        name: "News",
        path: "news",
        component: () => import("@/views/news/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        name: "NewsDetail",
        path: "news/detail",
        component: () => import("@/views/news/detail.vue"),
      },
      {
        name: "Notice",
        path: "notice",
        component: () => import("@/views/news/noticeIndex.vue"),
      },
      {
        name: "About",
        path: "about",
        component: () => import("@/views/about.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "Server",
        path: "server",
        component: () => import("@/views/server.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "ServerMedia",
        path: "/server/media",
        component: () => import("@/views/media/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "intro",
        path: "/intro",
        component: () => import("@/views/intro.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "Unit",
        path: "unit",
        component: () => import("@/views/unit.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        name: "UserCenter",
        path: "userCenter",
        component: () => import("@/views/user/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        name: "ApplyAuth",
        path: "applyAuth",
        component: () => import("@/views/user/applyAuth.vue"),
      },
      {
        name: "CompanyCenter",
        path: "companyCenter",
        component: () => import("@/views/company/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        name: "CreateOrder",
        path: "order/create",
        component: () => import("@/views/workOrder/create.vue"),
      },
      {
        name: "MyOrder",
        path: "order/my",
        component: () => import("@/views/workOrder/myOrder.vue"),
      },
    ],
  },
  {
    name: "Login",
    path: "/login",
    component: () => import("@/views/login/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory("/"),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) return savedPosition;
    return {
      top: 0,
      left: 0,
    };
  },
});

router.beforeEach(async (to) => {
  if (
    to.name === "UserCenter" ||
    to.name === "CompanyCenter" ||
    to.name === "MyOrder"
  ) {
    const userStore = useUserStore();
    await userStore.getUserInfo();
    if (!userStore.userInfo) {
      return false;
    }
  }
});

export default router;
